<?php
include '../../../wp-blog-header.php';

$siteurl = get_option('home');

$brand_array = array('base_url'=>'brands','slug_field'=>'brandurl','frequency'=>'daily','priority'=>'0.9');
$deal_array = array('base_url'=>'deals','slug_field'=>'camp_link','frequency'=>'yearly','priority'=>'0.6');
$tags_array = array('base_url'=>'','slug_field'=>'slug','frequency'=>'daily','priority'=>'1.0');
$sales_array = array('base_url'=>'deals','slug_field'=>'camp_link','frequency'=>'yearly','priority'=>'0.6');
$city_array = array('base_url'=>'localoffers','slug_field'=>'name','frequency'=>'daily','priority'=>'0.9');
$category_array = array('base_url'=>'','slug_field'=>'url','frequency'=>'daily','priority'=>'0.9');
$city_cat_array = array('base_url'=>'city_cat','slug_field'=>'name','frequency'=>'daily','priority'=>'0.9');

$podsarray = array('brand'=>$brand_array,'deal'=>$deal_array,'tags'=>$tags_array,'city'=>$city_array,'category'=>$category_array,'city_cat'=>$city_cat_array,'sales'=>$sales_array);
 
// Loop through pods with key-value
foreach ($podsarray as $k => $v)
{ 
    switch($k)
    {
      default:
        $Record = new Pod($k);
        $Record->findRecords('modified DESC', -1);
      break;
      
      case 'deal':
          $Record = new Pod('deal');
          $Record->findRecords('modified DESC', -1,"t.camp_end > ".fix_date()." && t.dealcheck='1' && t.sale='0' && t.daily='0'");  
      break;
      
      case 'sales':
          $Record = new Pod('deal');
          $Record->findRecords('modified DESC', -1,"t.camp_end > ".fix_date()." && t.sale='1' && t.daily='0'");
      break;
      
      case 'city':
          $Record = new Pod('city');
          $Record->findRecords('name ASC', -1);
      break;
      
      case 'category':
          $Record = new Pod('category');
          $Record->findRecords('name ASC', -1,'t.is_local = 0');
      break;
      
      case 'city_cat':
          $local_cat = new Pod('category');
          $local_cat->findRecords('name ASC', -1,'t.is_local = 1');
          $local_cat_rows = $local_cat->getTotalRows();
          
          $city = new Pod('city');
          $city->findRecords('name ASC', -1);
      break;
          
    }
    
    if ($Record->getTotalRows() > 0)
    {
        while ($Record->fetchRecord())
        {
            if(!empty($v['base_url']))
            {
              $slug = $siteurl . '/' . $v['base_url'] . '/' . strtolower(htmlspecialchars($Record->get_field($v['slug_field'])));
            }
            else
            {
              $slug = $siteurl . '/' . htmlspecialchars($Record->get_field($v['slug_field']));
            }
            
            $mod = htmlspecialchars($Record->get_field('modified'));
            
            if($v['base_url'] == 'localoffers' || $k == 'category')
            {
              $mod = htmlspecialchars(date('Y-m-d H:i:s'));  
            }
            
            
            $mod = strtotime($mod);
            $links['url'][]= $slug;
            $links['last_mod'][]= $mod;
            $links['change_freq'][]= $v['frequency'];
            $links['priority'][]= $v['priority'];
        }
    }
    
   
    if ($local_cat_rows > 0)
    {
        while ($local_cat->fetchRecord())
        {
           $category_url = $local_cat->get_field('url');
           while ($city->fetchRecord())
           {
            $city_name = $city->get_field('name');
            $slug = $siteurl.'/localoffers/'.strtolower(htmlspecialchars($city_name)).'/'.$category_url;
            $mod = htmlspecialchars(date('Y-m-d H:i:s'));
            $mod = strtotime($mod);
            $links['url'][]= $slug;
            $links['last_mod'][]= $mod;
            $links['change_freq'][]= $v['frequency'];
            $links['priority'][]= $v['priority'];
            
           }
           $city->resetPointer(0);
        }
    }
    
}

/*
echo "<pre>";
print_r($links);
echo "</pre>";
exit();
*/

$file = '/var/www/html/buzzr1/buzzr_sitemap.xml';
if (file_exists($file)) {
	unlink($file);
}
$podLinks = serialize($links);
//file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
$fileHandle = fopen($file, 'w') or die("can't open file");
//fclose($fileHandle);

if (is_writable($file))
{
    	// Write $pod_links to our opened file.
    	if (fwrite($fileHandle, $podLinks) === FALSE)
	{
		echo "Cannot write to file ($filename)";
		exit();
    	}
	echo "Success, wrote ($podLinks) to file ($file)";
	fclose($fileHandle);
}
else
{
	echo "The file $filename is not writable";
}
?>
